package com.smh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smh.common.CommonResult;
import com.smh.entity.User;
import com.smh.mapper.UserMapper;
import com.smh.service.UserService;
import com.smh.util.JwtUtil;
import com.smh.util.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public CommonResult<User> add(User user) {
        user.setUserPwd(MD5Util.getMD5(user.getUserPwd()));
        CommonResult<User> rs = new CommonResult<>();
        try {
            int k = userMapper.insert(user);

            if (k > 0) {
                rs.setCode(0);
                rs.setMsg("添加成功");
            } else {
                rs.setCode(-1);
                rs.setMsg("插入失败");
            }
            return rs;
        }catch (Exception e){
            rs.setCode(-1);
            rs.setMsg(e.getMessage());
            return rs;
        }
    }

    @Override
    public CommonResult edit(User user) {
        CommonResult rs = new CommonResult();
        try {


            int k = userMapper.updateById(user);
            if (k > 0) {
                rs.setCode(0);
                rs.setMsg("修改成功");
            } else {
                rs.setCode(-1);
                rs.setMsg("修改失败");
            }
            return rs;
        }catch (Exception e){
            rs.setCode(-1);
            rs.setMsg(e.getMessage());
            return rs;
        }
    }

    @Override
    public CommonResult list(String userName, String realName) {
        CommonResult rs = new CommonResult();
        try {
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            if (userName != null && !userName.isEmpty()) {
                queryWrapper.eq("user_name", userName);
            }
            if (realName != null && !realName.isEmpty()) {
                queryWrapper.eq("real_name", realName);
            }
            List<User> data = userMapper.selectList(queryWrapper);
            rs.setCode(0);
            rs.setMsg("查询成功");
            rs.setData(data);
            return rs;
        }catch (Exception e){
            rs.setCode(-1);
            rs.setMsg(e.getMessage());
            return rs;
        }
    }

    @Override
    public CommonResult listByPage(String userName, String realName, Integer page, Integer size) {
        CommonResult rs = new CommonResult();
        try{
            Page<User> pg = new Page<>(page,size);
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            if (userName != null && !userName.isEmpty()) {
                queryWrapper.eq("user_name", userName);
            }
            if (realName != null && !realName.isEmpty()) {
                queryWrapper.eq("real_name", realName);
            }
            userMapper.selectPage(pg,queryWrapper);
            List<User> data = pg.getRecords();
            long count = pg.getTotal();

            rs.setCode(0);
            rs.setMsg("查询成功");
            rs.setData(data);
            rs.setCount(count);
            return rs;

        }catch (Exception e){
            rs.setCode(-1);
            rs.setMsg(e.getMessage());
            return rs;
        }
    }


    @Override
    public CommonResult del(Integer id) {
        CommonResult rs = new CommonResult();
        try{
            int k = userMapper.deleteById(id);
            if(k>0){
                rs.setCode(0);
                rs.setMsg("删除成功");
                return rs;
            }else{
                rs.setCode(-1);
                rs.setMsg("删除失败");
                return rs;
            }
        }catch (Exception e){
            rs.setCode(-1);
            rs.setMsg(e.getMessage());
            return rs;
        }
    }

    @Override
    public User getUserById(Integer id) {
        User user = null;
        try {
            user = userMapper.selectById(id);
            return user;
        } catch (Exception e) {
            return null;
        }

    }

    @Override
    public CommonResult<String> queryByUsernameAndPwd(User user) {
        Map<String, Object> claims = new HashMap<>();
        claims.put("userName", user.getUserName());
        claims.put("userPwd", user.getUserPwd());
        String jwt = JwtUtil.getJwt(claims);


        CommonResult<String> cr = new CommonResult<>();
        String s1 = MD5Util.getMD5(user.getUserPwd());
        user.setUserPwd(s1);
        try {
            QueryWrapper<User> qw = new QueryWrapper<>();
            qw.eq("user_name", user.getUserName()).eq("user_pwd", user.getUserPwd());
            Integer i = userMapper.selectCount(qw);
            if (i == 1) {
                cr.setCode(0);
                cr.setMsg("登入成功");
                cr.setData(jwt);
            } else {
                cr.setCode(-1);
                cr.setMsg("登入失败");
            }
            return cr;
        } catch (Exception e) {
            cr.setCode(-1);
            cr.setMsg("登入失败");
            return cr;
        }
    }

    @Override
    public CommonResult<ArrayList<User>> queryAllByPage(Integer page, Integer size) {
        CommonResult<ArrayList<User>> cr = new CommonResult<>();
        try {
            Page<User> page1 = new Page<>(page, size);
            userMapper.selectPage(page1, null);
            Integer i = userMapper.selectCount(null);
            ArrayList<User> list = (ArrayList<User>) page1.getRecords();
            log.info("{}", i);
            cr.setCode(0);
            cr.setData(list);
            cr.setMsg("分页查询成功");
            cr.setCount(i);
            return cr;
        } catch (Exception e) {
            cr.setCode(-1);
            cr.setMsg("分页查询失败");
            return cr;
        }
    }

}
